
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 07/28/2012 12:42:55
-- Generated from EDMX file: C:\Users\The\Documents\Visual Studio 2010\Projects\SilverlightSdms\SilverlightSdms\SilverlightSdms.Web\Core.Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [dmsdb];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_MessageSentMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SentMessages] DROP CONSTRAINT [FK_MessageSentMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_UserMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_UserMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_UserPaycheck]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Paychecks] DROP CONSTRAINT [FK_UserPaycheck];
GO
IF OBJECT_ID(N'[dbo].[FK_RegionSchool]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Schools] DROP CONSTRAINT [FK_RegionSchool];
GO
IF OBJECT_ID(N'[dbo].[FK_TeamRegion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Regions] DROP CONSTRAINT [FK_TeamRegion];
GO
IF OBJECT_ID(N'[dbo].[FK_SchoolVisit]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Visits] DROP CONSTRAINT [FK_SchoolVisit];
GO
IF OBJECT_ID(N'[dbo].[FK_UserSentMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SentMessages] DROP CONSTRAINT [FK_UserSentMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_UserVisit]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Visits] DROP CONSTRAINT [FK_UserVisit];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserRole]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_UserUserRole];
GO
IF OBJECT_ID(N'[dbo].[FK_RoleUserRole]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_RoleUserRole];
GO
IF OBJECT_ID(N'[dbo].[FK_UserTeamMember]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TeamMembers] DROP CONSTRAINT [FK_UserTeamMember];
GO
IF OBJECT_ID(N'[dbo].[FK_TeamTeamMember]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TeamMembers] DROP CONSTRAINT [FK_TeamTeamMember];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Messages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Messages];
GO
IF OBJECT_ID(N'[dbo].[Paychecks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Paychecks];
GO
IF OBJECT_ID(N'[dbo].[Regions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Regions];
GO
IF OBJECT_ID(N'[dbo].[Schools]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Schools];
GO
IF OBJECT_ID(N'[dbo].[SentMessages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SentMessages];
GO
IF OBJECT_ID(N'[dbo].[Teams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Teams];
GO
IF OBJECT_ID(N'[dbo].[Visits]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Visits];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[UserRoles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserRoles];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[TeamMembers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TeamMembers];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Messages'
CREATE TABLE [dbo].[Messages] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Body] nvarchar(max)  NOT NULL,
    [Sender_Id] int  NOT NULL
);
GO

-- Creating table 'Paychecks'
CREATE TABLE [dbo].[Paychecks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ForMonth] int  NOT NULL,
    [Hours] float  NOT NULL,
    [HourlySalary] int  NOT NULL,
    [Fine] int  NOT NULL,
    [Bonus] int  NOT NULL,
    [IsCollected] bit  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Regions'
CREATE TABLE [dbo].[Regions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Team_Id] int  NULL
);
GO

-- Creating table 'Schools'
CREATE TABLE [dbo].[Schools] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Address] nvarchar(max)  NOT NULL,
    [Region_Id] int  NOT NULL
);
GO

-- Creating table 'SentMessages'
CREATE TABLE [dbo].[SentMessages] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [IsRead] bit  NOT NULL,
    [Message_Id] int  NOT NULL,
    [Recipient_Id] int  NOT NULL
);
GO

-- Creating table 'Teams'
CREATE TABLE [dbo].[Teams] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [TeamLeaderId] int  NOT NULL
);
GO

-- Creating table 'Visits'
CREATE TABLE [dbo].[Visits] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [StartTime] datetime  NOT NULL,
    [EndTime] datetime  NOT NULL,
    [IsDone] bit  NOT NULL,
    [School_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [DisplayName] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [Address] nvarchar(max)  NOT NULL,
    [Phone] int  NOT NULL,
    [HourlySalary] int  NOT NULL
);
GO

-- Creating table 'UserRoles'
CREATE TABLE [dbo].[UserRoles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [User_Id] int  NOT NULL,
    [Role_Id] int  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [RoleType] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'TeamMembers'
CREATE TABLE [dbo].[TeamMembers] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [User_Id] int  NOT NULL,
    [Team_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [PK_Messages]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Paychecks'
ALTER TABLE [dbo].[Paychecks]
ADD CONSTRAINT [PK_Paychecks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Regions'
ALTER TABLE [dbo].[Regions]
ADD CONSTRAINT [PK_Regions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Schools'
ALTER TABLE [dbo].[Schools]
ADD CONSTRAINT [PK_Schools]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'SentMessages'
ALTER TABLE [dbo].[SentMessages]
ADD CONSTRAINT [PK_SentMessages]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Teams'
ALTER TABLE [dbo].[Teams]
ADD CONSTRAINT [PK_Teams]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Visits'
ALTER TABLE [dbo].[Visits]
ADD CONSTRAINT [PK_Visits]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [PK_UserRoles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'TeamMembers'
ALTER TABLE [dbo].[TeamMembers]
ADD CONSTRAINT [PK_TeamMembers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Message_Id] in table 'SentMessages'
ALTER TABLE [dbo].[SentMessages]
ADD CONSTRAINT [FK_MessageSentMessage]
    FOREIGN KEY ([Message_Id])
    REFERENCES [dbo].[Messages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MessageSentMessage'
CREATE INDEX [IX_FK_MessageSentMessage]
ON [dbo].[SentMessages]
    ([Message_Id]);
GO

-- Creating foreign key on [Sender_Id] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_UserMessage]
    FOREIGN KEY ([Sender_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserMessage'
CREATE INDEX [IX_FK_UserMessage]
ON [dbo].[Messages]
    ([Sender_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Paychecks'
ALTER TABLE [dbo].[Paychecks]
ADD CONSTRAINT [FK_UserPaycheck]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserPaycheck'
CREATE INDEX [IX_FK_UserPaycheck]
ON [dbo].[Paychecks]
    ([User_Id]);
GO

-- Creating foreign key on [Region_Id] in table 'Schools'
ALTER TABLE [dbo].[Schools]
ADD CONSTRAINT [FK_RegionSchool]
    FOREIGN KEY ([Region_Id])
    REFERENCES [dbo].[Regions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RegionSchool'
CREATE INDEX [IX_FK_RegionSchool]
ON [dbo].[Schools]
    ([Region_Id]);
GO

-- Creating foreign key on [Team_Id] in table 'Regions'
ALTER TABLE [dbo].[Regions]
ADD CONSTRAINT [FK_TeamRegion]
    FOREIGN KEY ([Team_Id])
    REFERENCES [dbo].[Teams]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TeamRegion'
CREATE INDEX [IX_FK_TeamRegion]
ON [dbo].[Regions]
    ([Team_Id]);
GO

-- Creating foreign key on [School_Id] in table 'Visits'
ALTER TABLE [dbo].[Visits]
ADD CONSTRAINT [FK_SchoolVisit]
    FOREIGN KEY ([School_Id])
    REFERENCES [dbo].[Schools]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SchoolVisit'
CREATE INDEX [IX_FK_SchoolVisit]
ON [dbo].[Visits]
    ([School_Id]);
GO

-- Creating foreign key on [Recipient_Id] in table 'SentMessages'
ALTER TABLE [dbo].[SentMessages]
ADD CONSTRAINT [FK_UserSentMessage]
    FOREIGN KEY ([Recipient_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserSentMessage'
CREATE INDEX [IX_FK_UserSentMessage]
ON [dbo].[SentMessages]
    ([Recipient_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Visits'
ALTER TABLE [dbo].[Visits]
ADD CONSTRAINT [FK_UserVisit]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserVisit'
CREATE INDEX [IX_FK_UserVisit]
ON [dbo].[Visits]
    ([User_Id]);
GO

-- Creating foreign key on [User_Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_UserUserRole]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserRole'
CREATE INDEX [IX_FK_UserUserRole]
ON [dbo].[UserRoles]
    ([User_Id]);
GO

-- Creating foreign key on [Role_Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_RoleUserRole]
    FOREIGN KEY ([Role_Id])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoleUserRole'
CREATE INDEX [IX_FK_RoleUserRole]
ON [dbo].[UserRoles]
    ([Role_Id]);
GO

-- Creating foreign key on [User_Id] in table 'TeamMembers'
ALTER TABLE [dbo].[TeamMembers]
ADD CONSTRAINT [FK_UserTeamMember]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserTeamMember'
CREATE INDEX [IX_FK_UserTeamMember]
ON [dbo].[TeamMembers]
    ([User_Id]);
GO

-- Creating foreign key on [Team_Id] in table 'TeamMembers'
ALTER TABLE [dbo].[TeamMembers]
ADD CONSTRAINT [FK_TeamTeamMember]
    FOREIGN KEY ([Team_Id])
    REFERENCES [dbo].[Teams]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TeamTeamMember'
CREATE INDEX [IX_FK_TeamTeamMember]
ON [dbo].[TeamMembers]
    ([Team_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------